package com.hcr.action;


import java.sql.Timestamp;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.apache.struts2.ServletActionContext;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hcr.commons.CurrentTime;
import com.hcr.commons.GetApplicationContext;
import com.hcr.commons.IsAdmin;
import com.hcr.commons.PasswordToMD5;
import com.hcr.commons.Springfactory;
import com.hcr.commons.UserPage;
import com.hcr.model.Department;
import com.hcr.model.Role;
import com.hcr.model.User;
import com.hcr.service.DepartmentService;
import com.hcr.service.RoleService;
import com.hcr.service.UserService;
import com.hcr.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("userAction")
public class UserAction extends ActionSupport implements ModelDriven<User>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 9169187649305607038L;
	private UserService userService;
	private User user=new User();
	private DepartmentService departmentService;
	private UserPage userPage;
	private String role;
	


	


	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}


	private RoleService roleService;
	public RoleService getRoleService() {
		return roleService;
	}
	@Resource(name="roleService")
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public UserPage getUserPage() {
		return userPage;
	}
	@Resource(name="userPage")
	public void setUserPage(UserPage userPage) {
		this.userPage = userPage;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	@Resource(name="departmentService")
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String execute(){
		return "success";
	}
	public String showUsers(){
		List<User> users=this.userService.findAll();
		ActionContext context=ActionContext.getContext();
		context.put("users",users);
		
		return "success";
	}
	public String resetPassword(){
		this.userService.resetPassword(user.getId());
		return "showusers";
	}
	public String addUser(){
		this.getRoles();
		this.getDepartment();
		return "add";
	}
	public String add(){
		 Set<Role> set=new HashSet<Role>();
		 if(this.role!=null){
			String[] chkValues = this.role.split(", "); 
			
			 for(int i=0;i<chkValues.length;i++){
				Role r=this.roleService.findByName(chkValues[i]);
				set.add(r);
				Set<User> user1=r.getUsers();
				if(user1==null){
					user1=new HashSet<User>();
				}
				user1.add(this.user);
				this.roleService.update(r);
				
			 }
		}
		 
	
		Timestamp ts = new Timestamp(System.currentTimeMillis()); 
		this.user.setCreatetime(ts);
		this.user.setPassword(PasswordToMD5.MD5(this.user.getPassword()));
		this.user.setRoles(set);
		this.userService.addUser(user);
		
		return "showusers";
	}
	
	public String updateUser(){
		this.getDepartment();
		this.getRoles();
		return "update";
	}
	public String update(){
		 Set<Role> set=new HashSet<Role>();
		 if(this.role!=null){
			String[] chkValues = this.role.split(", "); 
			
			 for(int i=0;i<chkValues.length;i++){
				Role r=this.roleService.findByName(chkValues[i]);
				set.add(r);
				Set<User> user1=r.getUsers();
				if(user1==null){
					user1=new HashSet<User>();
				}
				user1.add(this.user);
				this.roleService.update(r);
				
			 }
		}
		 
		this.user.setCreatetime(CurrentTime.getCurrentTime());
		this.user.setPassword(PasswordToMD5.MD5(this.user.getPassword()));
		this.userService.updateUser(user);
		return "showusers";
	}
	public String delete(){
		this.userService.deleteById(user.getId());
		return "showusers";
	}
	public void getDepartment(){
		List<Department> departments=this.departmentService.findAll();
		ActionContext context=ActionContext.getContext();
		context.put("departments", departments);
	}
	public User getModel() {
		return this.user;
	}
	public String firstPage(){
		System.out.println("访问firstpage方法");
		UserService userService = (UserService) GetApplicationContext.getInstance().getBean("userService");
		this.userPage.setUserService(userService);
		List<User> list=this.userPage.firstPage();
		ActionContext context=ActionContext.getContext();
		context.put("users", list);
		return "success";
		
		
		
	}
	public String showUser_base(){
		return "showuser_base";
		
	}
	public String nextPage(){
		List<User> list=this.userPage.nextPage();
		ActionContext context=ActionContext.getContext();
		context.put("users", list);
		return "success";
	}
	public String previousPage(){
		List<User> list=this.userPage.previousPage();
		ActionContext context=ActionContext.getContext();
		context.put("users", list);
		return "success";
	}
	public String findById(){
		User user=this.userService.findById(this.user.getId());
		ActionContext context=ActionContext.getContext();
		context.put("users", user);
		return "showbyid";
	}
	
	
	public void getRoles(){
		List<Role> roles=this.roleService.showAll();
		ActionContext context=ActionContext.getContext();
		context.put("roles", roles);
	}
	public String updateUser_base(){
		return "updateuser_base";
	}
	public String update_base(){
		this.update();
		return "update_base";
	}
	/*@PostConstruct
	public void init(){
		System.out.println("已经调用init方法&&&&&&&&");
		//UserService userService = (UserService) GetApplicationContext.getInstance().getBean("userService");
		this.userPage.setUserService(userService);
	}*/
	/*@PostConstruct
	public void start(){
		UserService userService = (UserService) GetApplicationContext.getInstance().getBean("userService");
		this.userPage.setUserService(userService);
	}*/
	
	
}
